package com.dhecp.project.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.system.domain.SysDictionaryRelationForModule;
import com.dhecp.project.system.mapper.SysDictionaryRelationForModuleMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysDictionaryRelationForModuleService;

/**
 * 功能调入信息  业务实现层
 * @author DH-lwl
 * @version 2019年11月26日
 */
@Service
public class SysDictionaryRelationForModuleServiceImpl implements SysDictionaryRelationForModuleService {

	@Autowired
	private SysDictionaryRelationForModuleMapper dictionaryRelationForModuleMapper;
	
	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;

	/**
	 * 查询所有功能调入信息列表
	 * @param params
	 * @return List-DictionaryRelationForModule
	 * @author DH-lwl
	 * @throws Exception 
	 */
	@Override
	public List<SysDictionaryRelationForModule> queryDictionaryRelationForModule(Map<String, Object> params) throws Exception {
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));
		if (params.get("field") == null || params.get("field") == ""){
			String orderBy = "relationOrder asc";
			PageUtils.startPageOrder(orderBy);
		}else{
			PageUtils.startPage();	
		}	
		return dictionaryRelationForModuleMapper.findDictionaryRelationForModule(params);
	}

	/**
	 * 通过rModuleTable查询指定模块的调入功能列表
	 * @param rModuleTable 必要参数：指定功能唯一标识
	 * @return List - SysDictionaryRelationForModule
	 * @author DH-Xxj
	 */
	@Override
	public List<SysDictionaryRelationForModule> queryDictionaryRelationForModuleByRModuleTable(String rModuleTable) {
		return dictionaryRelationForModuleMapper.findDictionaryRelationForModuleByRModuleTable(rModuleTable);
	}
	
	/**
	 * 通过guid查询单条信息
	 * @param guid
	 * @return 实体DictionaryRelationForModule
	 * @author DH-lwl
	 */
	@Override
	public SysDictionaryRelationForModule queryDictionaryRelationForModuleByGuid(String guid) {
		return dictionaryRelationForModuleMapper.findDictionaryRelationForModuleByGuid(guid);
	}

	/**
	 * 新增调入信息
	 * @param dictionaryRelationForModuleItems
	 * @return MessageModel
	 * @author DH-lwl
	 */
	@Override
	public AjaxResult insertDictionaryRelationForModule(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<SysDictionaryRelationForModule> dictionaryRelationForModuleList = JSON.parseArray(params.get("dictionaryRelationForModuleItems").toString(), SysDictionaryRelationForModule.class);
		
		if (dictionaryRelationForModuleList == null || dictionaryRelationForModuleList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysDictionaryRelationForModule dictionaryRelationForModule : dictionaryRelationForModuleList) {
			dictionaryRelationForModuleMapper.insertDictionaryRelationForModule(dictionaryRelationForModule);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 通过guid删除调入信息
	 * @param dictionaryRelationForModuleItems
	 * @return MessageModel
	 * @author DH-lwl
	 */
	@Override
	public AjaxResult deleteDictionaryRelationForModuleByGuid(Map<String, Object> params) {
		List<SysDictionaryRelationForModule> dictionaryRelationForModuleList = JSON.parseArray(params.get("dictionaryRelationForModuleItems").toString(), SysDictionaryRelationForModule.class);
		if (dictionaryRelationForModuleList == null || dictionaryRelationForModuleList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysDictionaryRelationForModule dictionaryRelationForModule : dictionaryRelationForModuleList) {
			dictionaryRelationForModuleMapper.deleteByGuid(dictionaryRelationForModule);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 通过guid修改调入信息
	 * @param dictionaryRelationForModuleItems
	 * @return MessageModel
	 * @author DH-lwl
	 */
	@Override
	public AjaxResult updateDictionaryRelationForModuleByGuid(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<SysDictionaryRelationForModule> dictionaryRelationForModuleList = JSON.parseArray(params.get("dictionaryRelationForModuleItems").toString(), SysDictionaryRelationForModule.class);
		if (dictionaryRelationForModuleList == null || dictionaryRelationForModuleList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysDictionaryRelationForModule dictionaryRelationForModule : dictionaryRelationForModuleList) {
			dictionaryRelationForModuleMapper.updateByGuid(dictionaryRelationForModule);
		}
		return AjaxResult.success("操作成功");
	}
	
}
